//
//  PatterySurplusRecordVC.m
//  Toyota
//
//  Created by ld on 2024/6/25.
//

#import "PatterySurplusRecordVC.h"
#import "PatterySurplusRecordCell.h"
#import "FilterDataTableView.h"
#import "NoDataView.h"
#import "BRPickerView.h"
#import "PopUpTipsView.h"
#import <LSTPopView.h>
#import "Definition.h"
#import "Helper.h"
@interface PatterySurplusRecordVC ()<UITableViewDelegate, UITableViewDataSource>{
    UIView * filterView;
    
    NSString * beginTime;
    NSString * endTime;
    NSString * timeStr;
    NSString * powerSupplyStr;
    
    UIButton * timeBtn;
    UIButton * powerSupplyBtn;
    
    UIView * parentPopView;
    
    NoDataView * noDataView;
}

@property (nonatomic, strong)NSMutableArray * powerSupplyArr;  //供电量
@property (strong, nonatomic) UITableView * tableView;
@property (nonatomic, strong) NSMutableArray *dataArr;
@end

@implementation PatterySurplusRecordVC

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor whiteColor];
    self.title = @"供电履历";
    // 获取当前时间
    NSDate *currentDate = [NSDate date];
     
    // 获取7天前的时间
    NSDate *sevenDaysAgo = [NSDate dateWithTimeIntervalSinceNow: -6 * 24 * 60 * 60];
     
    // 创建日期格式化器
    NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
    [dateFormatter setDateFormat:@"yyyy-MM-dd"];
     
    // 将日期转换为字符串
    endTime = [dateFormatter stringFromDate:currentDate];
    beginTime = [dateFormatter stringFromDate:sevenDaysAgo];
     
    [self initUI];
    [self reloadData];
    // Do any additional setup after loading the view.
}

-(void)viewWillDisappear:(BOOL)animated
{
    [super viewWillDisappear: animated];
    [LSTPopView removeAllPopView];
}

-(void)initUI {
    filterView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, ScreenWidth, 45)];
    filterView.backgroundColor = [UIColor whiteColor];
    [self.view addSubview:filterView];
    _powerSupplyArr = [NSMutableArray arrayWithObjects:@"全部", @"0-10kwh", @"10-20kwh", @"20-30kwh", @"30-40kwh", @"40-50kwh", @"50-100kwh", nil];
    NSArray * filterArr = @[@"供电日期", @"供电量"];
    for (int i = 0; i < filterArr.count; i++) {

        UIButton * choiceBtn = [UIButton initWithButton:CGRectMake( 20 + (filterView.customWidth - 40) / 2. * i, 0, (filterView.customWidth - 40) / 2., filterView.customHeight) text:filterArr[i] font:fontSize(14) textColor:kColor686868 selectedTextColor:kColor141414 normalImg:[UIImage imageNamed:@"choice_down"] highImg:nil selectedImg:[UIImage imageNamed:@"choice_up"]];
        [choiceBtn layoutButtonWithEdgeInsetsStyle:(ButtonEdgeInsetsStyleRight) imageTitleSpace:3];
//        choiceBtn.titleLabel.adjustsFontSizeToFitWidth = YES;
        choiceBtn.tag = 2000 + i;
        if (i == 0) {
            timeBtn = choiceBtn;
            [choiceBtn addTarget:self action:@selector(timeBtnAction:) forControlEvents:(UIControlEventTouchUpInside)];
            if (beginTime.length && endTime.length) {
                timeStr = [NSString stringWithFormat:@"%@-%@", beginTime, endTime];
                [timeBtn setTitle:timeStr forState:(UIControlStateNormal)];
                [timeBtn layoutButtonWithEdgeInsetsStyle:(ButtonEdgeInsetsStyleRight) imageTitleSpace:3];
            }
        }else if (i == 1) {
            powerSupplyBtn = choiceBtn;
            [choiceBtn addTarget:self action:@selector(powerSupplyAction:) forControlEvents:(UIControlEventTouchUpInside)];
        }
        [filterView addSubview:choiceBtn];
    }
    
    self.tableView = [[UITableView alloc] init];
    self.tableView.backgroundColor = kColorFFFFFF;
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    _tableView.tableFooterView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, ScreenWidth, 5)];
    [self.view addSubview:self.tableView];
    [self.tableView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.mas_equalTo(0);
        make.bottom.mas_equalTo(-CYSafeAreaBottom);
        make.top.equalTo(filterView.mas_bottom);
    }];
}

- (void)reloadData {
//    noDataView = [[NoDataView alloc]initWithFrame:CGRectMake(0, 0, ScreenWidth, ScreenHeight - Height_StatusBar - 44)];
//    [noDataView hasImg:YES andTitleStr:@"暂无数据" andImgStr:@"noDataImg" andHeight:CYScalXH(170)];
//    [self.tableView addSubview:noDataView];
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return 4;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 80;
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    PatterySurplusRecordCell *cell = [tableView dequeueReusableCellWithIdentifier:@"PatterySurplusRecordCell"];
    if (!cell) {
        cell = [[PatterySurplusRecordCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"PatterySurplusRecordCell"];
    }
    while ([cell.contentView.subviews lastObject] != nil)
    {
        [(UIView*)[cell.contentView.subviews lastObject] removeFromSuperview];
    }
//    tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    cell.backgroundColor = kColorFFFFFF;
    [cell setSelectionStyle:UITableViewCellSelectionStyleNone];
    
    [cell setData:nil index:indexPath.row];
    return cell;
}


//供电日期
-(void)timeBtnAction:(UIButton *)sender {
    sender.selected = !sender.selected;
    [self timeSelectionBoxViewWithTitle:@"选择开始日期" andTimeType:@"beginTime" andMinData:nil];
}

//供电量
-(void)powerSupplyAction:(UIButton *)sender {
    sender.selected = !sender.selected;
    FilterDataTableView * view = [[FilterDataTableView alloc]initWithFrame:CGRectMake(0, filterView.customBottom + Height_StatusBar + 44, ScreenWidth, _powerSupplyArr.count > 7 ? 340 : 47 * _powerSupplyArr.count + 10)];
    view.dateArray = _powerSupplyArr;
    view.selectedCell = powerSupplyStr;
    [view round:10 RectCorners:(UIRectCornerBottomLeft | UIRectCornerBottomRight)];
    [LSTPopView removeAllPopView];
    
    [parentPopView removeFromSuperview];
    parentPopView = [[UIView alloc]initWithFrame:CGRectMake(0, filterView.customBottom + Height_StatusBar + 44, ScreenWidth, ScreenHeight)];
    parentPopView.backgroundColor = [UIColor clearColor];
    [[UIApplication sharedApplication].keyWindow addSubview:parentPopView];
    LSTPopView *popView = [LSTPopView initWithCustomView:view parentView:parentPopView popStyle:LSTPopStyleFade dismissStyle:LSTDismissStyleFade];
    
    __block LSTPopView *blockFooter = popView;
    popView.popDuration = 0.5;
    popView.hemStyle = LSTHemStyleTop;
    popView.dismissDuration = 0.5;
    popView.bgAlpha = 0.5;
    [popView pop];
    popView.bgClickBlock = ^{
        [blockFooter dismiss];
        [self->parentPopView removeFromSuperview];
        sender.selected = NO;
    };
    view.choiceBtnClick = ^(NSString * _Nonnull btnStr) {
        if ([btnStr isEqualToString:@"全部"]) {
            btnStr = @"供电量";
        }
        self->powerSupplyStr = btnStr;
        [sender setTitle:btnStr forState:(UIControlStateNormal)];
        [sender layoutButtonWithEdgeInsetsStyle:(ButtonEdgeInsetsStyleRight) imageTitleSpace:3];
        [self->parentPopView removeFromSuperview];
        sender.selected = NO;
    };
}

-(void)timeSelectionBoxViewWithTitle:(NSString *)title andTimeType:(NSString *)time andMinData:(NSDate *)minData {
    // 1.创建日期选择器
    BRDatePickerView *datePickerView = [[BRDatePickerView alloc]init];
    // 2.设置属性
    datePickerView.pickerMode = BRDatePickerModeYMD;
    datePickerView.title = title;
    if (minData) {
        datePickerView.minDate = minData;
    }
    datePickerView.maxDate = [NSDate date];
    datePickerView.isAutoSelect = YES;
    
    datePickerView.resultActionBlock = ^(NSDate * _Nullable selectDate, NSString * _Nullable selectValue) {
        if ([time isEqualToString:@"beginTime"]) {
            self->beginTime = selectValue;
            [self timeSelectionBoxViewWithTitle:@"选择结束日期" andTimeType:@"endTime" andMinData:selectDate];
        }else if ([time isEqualToString:@"endTime"]) {
            self->endTime = selectValue;
            self->timeStr = [NSString stringWithFormat:@"%@-%@", self->beginTime, self->endTime];
            [self->timeBtn setTitle:self->timeStr forState:(UIControlStateNormal)];
            [self->timeBtn layoutButtonWithEdgeInsetsStyle:(ButtonEdgeInsetsStyleRight) imageTitleSpace:3];
            self->timeBtn.selected = NO;
        }
    };
  
    // 设置自定义样式
    BRPickerStyle *customStyle = [[BRPickerStyle alloc]init];
    customStyle.pickerColor = [UIColor whiteColor];
    customStyle.titleTextColor = kColor141414;
    customStyle.hiddenTitleLine = YES;
    customStyle.titleTextFont = fontBold(16);
    customStyle.pickerTextColor = kColor141414;
    customStyle.separatorColor = kColor969696;
    customStyle.separatorColor = kColorEDEDED;
    datePickerView.pickerStyle = customStyle;

    // 3.显示
    [datePickerView show];
}



/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
